package com.zhongxi.guoyuan.utils;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

/**
 * Created by Administrator on 2018/2/6.
 */

public abstract class EndLessRecycleScrollListener extends RecyclerView.OnScrollListener {

    private boolean mIsSlidingUpward;

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        LinearLayoutManager mManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        //不滑动时
        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            int mLastPosition = mManager.findLastCompletelyVisibleItemPosition();
            int mItemCount = mManager.getItemCount();
            if ((mLastPosition == mItemCount - 1) && mIsSlidingUpward) {
                LoadMore();
            }
        }
    }

    /**
     * @param recyclerView
     * @param dx           向左滑动为正
     * @param dy           //向下滑动为正
     */

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        mIsSlidingUpward = dy > 0;
    }

    /**
     * 记载更多
     */
    public abstract void LoadMore();
}
